app\reviews[slug]\page.jsx
// 產生靜態頁面
export async function generateStaticParams() {
return [
{ slug: "hellblade" },
{ slug: "hollow-knight" },
]
}
變成單獨的函數~回傳 slug
lib\reviews.js
//單獨取出slug
export async function getSlugs() {
const files = await readdir("./content/reviews");
return files
.filter((file) => file.endsWith(".md"))
.map((file) => file.slice(0, -".md".length));
}
// 註 getSlugs() 是拿 getReviews() 程式碼來改的~ 為了不要重複使用程式碼 這邊也要修改一下
export async function getReviews() {
const slugs = await getSlugs();
const reviews = [];
for (const slug of slugs) {
const review = await getReview(slug);
reviews.push(review);
}
return reviews;
}
大叔的鐵人賽第十五天結束 :)